<template>
  <div class='m-sys_balanceOfProject_attr_attr'>
    <div style='padding-bottom: 20px;'>
      <el-button type='primary' @click='add'>添加属性</el-button>
    </div>
    <a-table :data='data' :cols='tableKeys'>
      <el-table-column align="center" label="操作" width='260' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='deleteObj(scope.row.class_id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>

    <add-attr-dialog ref='addAttrDialog' @update='search' :id='id' />
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import addAttrDialog from './components/addAttrDialog'

export default {
  name: 'sys_balanceOfProject_attr_attr',

  components: {
    addAttrDialog
  },

  data() {
    return {
      tableKeys: [
        { prop: 'class_name', label: '属性值', minWidth: '100' }
      ],
      data: [],
      id: '',
      ifRequesting: false
    }
  },

  mounted() {
    if (this.isFirst) {
      this.isFirst = false
      return false
    }
    const { id } = this.$route.query
    if (!id) {
      return this.$router.replace({ name: 'sys_balanceOfProject' })
    }
    this.id = id
    this.search()
  },

  activated() {
    const { id } = this.$route.query
    if (!id) {
      return this.$router.replace({ name: 'sys_balanceOfProject' })
    }
    this.id = id
    this.search()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    search() {
      this.$refs.page.changePage()
    },

    async fetchData(aPageNum, aPageSize) {
      return this.$api.inventGetCostClasses({
        aCostId: this.id
      })
    },

    getData(data) {
      this.data = data
    },

    add() {
      this.$refs.addAttrDialog.$show()
    },

    set(data) {
      this.$refs.addAttrDialog.$setData(JSON.parse(JSON.stringify(data)))
    },

    choose() {
    },

    deleteObj(id) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该属性吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.inventDeleteCostClass({ aCostId: this.id, aClassId: id })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    }
  }
}
</script>

<style lang='less'>

</style>
